﻿using System.Text;

namespace configstore
{
    using configstore.CSM.Host;
    using ExcelConfig;
    internal class Program
    {
        static async Task<int> Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine("Welcome config store center");

            if (args?.Count(t => t.Equals("cmd", StringComparison.OrdinalIgnoreCase)) > 0)
            {

                Console.WriteLine("Console Mode ON.");
                string cmd;
                do
                {
                    Console.Write("# ");
                    cmd = Console.ReadLine() ?? string.Empty;
                    switch (cmd.ToLower())
                    {
                        case "password":
                        case "pwd":
                        case "pass":
                        case "passwd":
                            Console.Write("entry new password: ");
                            var pwd = Console.ReadLine() ?? string.Empty;
                            if (string.IsNullOrEmpty(pwd)) pwd = "123456";
                            AppConfig.WriteText("验证密码", pwd);
                            continue;
                    }
                    break;
                } while (true);

                return 0;
            }

            try
            {
                using IWebHost host = new WebHost();
                await host.Start();

                lock (host) while (host.State == TouchSocket.Sockets.ServerState.Running) Monitor.Wait(host, 1000);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return -1;
            }
            Console.WriteLine("End");
            return 0;
        }
    }
}
